package core;

import java.util.ArrayList;
import java.util.List;

public class Concourt {
	
	private int GameInNbPoints;
	private int inNbGame;
	private int nbGamePlayed;
	private List<Team> teams;

	
	public Concourt(int gameInNbPoints,int nbGame){
		this.GameInNbPoints=gameInNbPoints;
		inNbGame=nbGame;
		nbGamePlayed=0;
		teams=new ArrayList<Team>();
	}
	
	public Concourt(int inNbPoint, int nbGamePlayed, int inNbGame) {
		this(inNbPoint,inNbGame);
		this.nbGamePlayed=nbGamePlayed;
	}

	public int getInNbGame(){
		return inNbGame;
	}
	
	public int getNbGamePlayed(){
		return nbGamePlayed;
	}
	
	public void addTeam(String playersNames){
		addTeam(new Team(playersNames));
	}

	public void addTeam(Team team) {
		teams.add(team);		
	}
	
	public int getGameWinInNbPoints(){
		return GameInNbPoints;
	}
	
	public boolean haveSteelMatch(){
		return (nbGamePlayed<inNbGame);
	}

	public Team[] getClassement(){
		Team[] res = new Team[teams.size()];
		for(int i=0;i<teams.size();i++)
			res[i]=teams.get(i);
		for(boolean continu=true;continu;){
			continu=false;
			for(int i=0;i<res.length-1;i++){
				if(Team.isBetterThan(res[i+1], res[i])<0){
					Team aux = res[i+1];
					res[i+1]=res[i];
					res[i]=aux;
					continu=true;
				}
			}
		}		
		return res;
	}
	
	public List<Match> getMatchs(){
		nbGamePlayed++;
		Team[] cl=getClassement();
		List<Team> aux = new ArrayList<Team>();
		for (int i= 0;i<cl.length;i++)
			if(!cl[i].haveAbandonned())
				aux.add(cl[i]);
		cl= new Team[aux.size()];
		for (int i= 0;i<aux.size();i++)
			cl[i]=aux.get(i);
		return new LocalSearch(cl).search(this.GameInNbPoints);
	}
	
}
